<?php

namespace common\modules\wechat\models;

use common\behaviors\TimestampBehavior;
use common\behaviors\UserRecordBehavior;
use common\components\ActiveRecord;
use common\modules\wechat\components\MpWechat;
use Yii;
use yii\db\Expression;
use yii\helpers\Url;

/**
 * This is the model class for table "{{%wechat_account}}".
 *
 * @property integer $id
 * @property string  $name
 * @property string  $token
 * @property string  $access_token
 * @property string  $account
 * @property string  $original
 * @property integer $type
 * @property string  $key
 * @property string  $secret
 * @property string  $encoding_aes_key
 * @property string  $avatar
 * @property string  $qrcode
 * @property string  $address
 * @property string  $description
 * @property string  $username
 * @property integer $status
 * @property string  $password
 * @property integer $created_at
 * @property integer $updated_at
 * @property integer $created_by
 * @property integer $updated_by
 */
class Account extends ActiveRecord
{

    /**
     * 未激活状态
     */
    const STATUS_INACTIVE = 0;

    /**
     * 激活状态
     */
    const STATUS_ACTIVE = 1;

    /**
     * 删除状态
     */
    const STATUS_DELETED = -1;

    /**
     * 普通订阅号
     */
    const TYPE_SUBSCRIBE = 0;

    /**
     * 认证订阅号
     */
    const TYPE_SUBSCRIBE_VERIFY = 1;

    /**
     * 普通服务号
     */
    const TYPE_SERVICE = 2;

    /**
     * 认证服务号
     */
    const TYPE_SERVICE_VERIFY = 3;

    /**
     * 公众号类型列表
     *
     * @var array
     */
    public static $types = [
        self::TYPE_SUBSCRIBE        => '订阅号',
        self::TYPE_SERVICE          => '服务号',
        self::TYPE_SUBSCRIBE_VERIFY => '认证订阅号',
        self::TYPE_SERVICE_VERIFY   => '认证服务号',
    ];

    public static $statuses = [
        self::STATUS_INACTIVE => '未接入',
        self::STATUS_ACTIVE   => '已接入',
        self::STATUS_DELETED  => '已删除',
    ];

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%wechat_account}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'account', 'original', 'type', 'key', 'secret'], 'required', 'except' => ['avatarUpload', 'qrcodeUpload']],
            [['type', 'status'], 'integer', 'except' => ['avatarUpload', 'qrcodeUpload']],
            [['name', 'original', 'username'], 'string', 'max' => 40, 'except' => ['avatarUpload', 'qrcodeUpload']],
            [['token', 'password'], 'string', 'max' => 32, 'except' => ['avatarUpload', 'qrcodeUpload']],
            [['address', 'description', 'avatar', 'qrcode'], 'string', 'max'    => 255, 'except' => ['avatarUpload', 'qrcodeUpload']],
            [['account'], 'string', 'max' => 30, 'except' => ['avatarUpload', 'qrcodeUpload']],
            [['key', 'secret'], 'string', 'max' => 50, 'except' => ['avatarUpload', 'qrcodeUpload']],
            [['encoding_aes_key'], 'string', 'max' => 43, 'except' => ['avatarUpload', 'qrcodeUpload']],

            [['avatar'], 'file', 'extensions' => 'gif, jpg', 'on' => 'avatarUpload'],
            [['qrcode'], 'file', 'extensions' => 'gif, jpg', 'on' => 'qrcodeUpload'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id'               => '公众号ID',
            'name'             => '公众号名称',
            'token'            => '微信服务Token(令牌)',
            'access_token'     => 'AccessToken(访问令牌)',
            'account'          => '微信号',
            'original'         => '原始ID',
            'type'             => '公众号类型',
            'key'              => 'AppID(应用ID)',
            'secret'           => 'AppSecret(应用密钥)',
            'encoding_aes_key' => '消息加密秘钥EncodingAesKey',
            'avatar'           => '头像地址',
            'qrcode'           => '二维码地址',
            'address'          => '所在地址',
            'description'      => '公众号简介',
            'username'         => '微信官网登录名(邮箱)',
            'status'           => '状态',
            'password'         => '微信官网登录密码',
            'created_at'       => '创建时间',
            'updated_at'       => '修改时间',
            'created_by'       => '创建用户',
            'updated_by'       => '修改用户',

            'apiUrl' => 'API地址',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeHints()
    {
        return [
            'apiUrl' => '请复制该内容填写到微信后台->开发者中心->服务器配置并确定Token和EncodingAesKey和微信后台的设置保持一致.',
        ];
    }

    /**
     * @inheritdoc
     */
    public static function find()
    {
        return Yii::createObject(AccountQuery::className(), [get_called_class()]);
    }

    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        $behaviors = parent::behaviors();

        $behaviors[] = [
            'class'              => TimestampBehavior::className(),
            'createdAtAttribute' => 'created_at',
            'updatedAtAttribute' => 'updated_at',
            'value'              => new Expression('NOW()'),
        ];

        // 后台才需要这个
        if (IS_BACKEND)
        {
            $behaviors[] = [
                'class' => UserRecordBehavior::className(),
            ];
        }

        return $behaviors;
    }

    /**
     * 返回公众号微信接口链接
     *
     * @param boolean|string $scheme the URI scheme to use in the generated URL:
     * @return string
     */
    public function getApiUrl($scheme = true)
    {
        return Url::toRoute([
            '/wechat/' . \common\modules\wechat\Module::current()->apiRoute,
            'id' => $this->id,
        ], $scheme);
    }

    /**
     * @var MpWechat
     */
    private $_sdk;

    /**
     * 获取实例化后的公众号SDK类
     *
     * @return MpWechat
     */
    public function getSdk()
    {
        if ($this->_sdk === null)
        {
            $this->_sdk = Yii::createObject(MpWechat::className(), [$this]);
        }
        return $this->_sdk;
    }
}
